<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<%! String setstridss = "0";  %>
<%
	String bkidt = request.getParameter("csflid");
	String bkidts = request.getParameter("csflids");
	if(bkidt == null){
		String strpldebkids = (String) request.getSession().getAttribute("strpldebkid");
		System.out.println("strpldebkid:" + strpldebkids);
		bkidt = strpldebkids;
	}
	System.out.println("###################");
	System.out.println("bkid:" + bkidt);
	System.out.println("bkids:" + bkidts);
	System.out.println("###################");
	HttpSession sessiong = request.getSession();
    sessiong.setAttribute("bokedeid", bkidt);  
    System.out.println(bkidt);
%>
<%!
	    private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	    private static final String DB_USER = "root";
	    private static final String DB_PWD = "ZLWL";
		String setidididp = "0";
		String settitlep = "0";
		String setauthor_idp = "0";
		String setpublish_timep = "0";
		String setread_countp = "0";
		String setcomment_countp = "0";
		String setcontentp = "0";
		String settypeidp = "0";
		Map<String, String> hashMapa = new HashMap<String, String>();
	%>
<%
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn;
		PreparedStatement pst;
		try {
			conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			Statement stmt = conn.createStatement();
			String sqlo = "select * from blog where id = " + bkidt;
			ResultSet rso = stmt.executeQuery(sqlo);
			String setdits = "0";
			while (rso.next()) {
				String getdits = rso.getString("id");
				setdits = getdits;
			}
			System.out.println("blog id:" + setdits);
			System.out.println("**********************************");
			String sqlp = "select * from blog where id = " + bkidt;
			ResultSet rsp = stmt.executeQuery(sqlp);
			while (rsp.next()) {
				String getidididp = rsp.getString("id");
				String gettitlep = rsp.getString("title");
				String getauthor_idp = rsp.getString("author_id");
				String getpublish_timep = rsp.getString("publish_time");
				String getread_countp = rsp.getString("read_count");
				String getcomment_countp = rsp.getString("comment_count");
				String getcontentp = rsp.getString("content");
				String gettypeidp = rsp.getString("type_id");
				setidididp = getidididp;
				settitlep = gettitlep;
				setauthor_idp = getauthor_idp;
				setpublish_timep = getpublish_timep;
				setread_countp = getread_countp;
				setcomment_countp = getcomment_countp;
				setcontentp = getcontentp;
				settypeidp = gettypeidp;
			}
			int readcou = Integer.parseInt(setread_countp) + 1;
			setread_countp = Integer.toString(readcou);
			String sqlh = "update blog set read_count = " + readcou + " where id = ?";
			pst = conn.prepareStatement(sqlh);
			pst.setString(1, bkidt);
			pst.executeUpdate();
			System.out.println("blog id:" + setidididp);
			System.out.println("blog title:" + settitlep);
			System.out.println("blog author_id:" + setauthor_idp);
			System.out.println("blog publish_time:" + setpublish_timep);
			System.out.println("blog read_count:" + setread_countp);
			System.out.println("blog comment_count:" + setcomment_countp);
			System.out.println("blog content:" + setcontentp);
			System.out.println("blog type_id:" + settypeidp);
			System.out.println("**********************************");
			rsp.close();
			rso.close();
			stmt.close();
			pst.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	%>
<title><%=settitlep%></title>
<style type="text/css">
a {
	text-decoration: none;
	color: black;
}

a:hover {
	color: red;
}

.yhmyd {
	position: absolute;
	left: 770px;
	top: 30px;
}

.yhmydzy {
	position: absolute;
	left: 800px;
	top: 30px;
}

div {
	border: 1px solid gray;
	width: 700px;
	height: 800px;
	background-color: #F5F5F5;
}

.xhxyd {
	position: relative;
	top: 50px;
}

.divadiv {
	position: relative;
	left: 100px;
	top: 100px;
	padding: 30px;
}

.pbtime {
	color: gray;
}

.liang {
	position: absolute;
	top: 820px;
}

.ping {
	position: absolute;
	left: 150px;
}

.pdbl {
	position: relative;
	left: 100px;
}

.aniu {
	border: 1px solid gray;
	background-color: gray;
}

.plfk {
	border: 1px solid gray;
	width: 700px;
	height: 200px;
	position: relative;
	left: 100px;
}

.plfkleft {
	border: 1px solid gray;
	width: 100px;
	height: 200px;
	position: relative;
	top: -1px;
	float: left;
}

.plfkright {
	width: 600px;
	height: 200px;
}

.pltimesj {
	position: absolute;
	top: 180px;
	left: 500px;
	color: gray;
}
.deletepl{
    position: absolute;
	top: 170px;
}
.sures{
    position: absolute;
	top: 140px;
	border: 1px solid gray;
}
</style>
</head>
<body onmousemove="pos()">
	<%
		String strSessionj = (String) request.getSession().getAttribute("namet");
		System.out.println("typelist:" + strSessionj);
		if(strSessionj == null){
			response.getWriter().append("<a href='http://localhost:8089/user_login.html' class='yhmyd'>登录</a>");
			response.getWriter().append("<span class='yhmydzy'><a href='mainpage.jsp'>&nbsp;&nbsp;&nbsp;返回主页</a></span>");	
		} else {
			response.getWriter().append("<a href='http://localhost:8089/user_personalpage.jsp' class='yhmyd'>");
			response.getWriter().append(strSessionj);
			response.getWriter().append("</a>");
			response.getWriter().append("<span class='yhmydzy'><a href='mainpage_login.jsp'>返回主页</a></span>");	
		}
	%>
	<!--  
	<a href="http://localhost:8089/user_login.html" class="yhmyd">登录</a>
	<a href="http://localhost:8089/user_personalpage.jsp" class="yhmyd"><%=strSessionj%></a>
	<span><a href="mainpage_login.jsp" class="yhmydzy">返回主页</a></span>
	-->
	<!--   <span><a href="http://localhost:8089/blog_single_typelist.jsp" class="fhsyy">返回上一页</a></span>   -->
	<!-- 
	<hr class="xhxyd">
	<div>
	    <h1>title</h1>
	    <span class="pbtime">(publish_time)</span>
	    <p>content</p>
	    <span class="liang">阅读量(read_count)</span><span class="liang">评论量(comment_count)</span>
	</div>
	<br><br><br><br><br><br><br>
	-->
	<%
	    response.getWriter().append("<hr class='xhxyd'>");
	    response.getWriter().append("<div class='divadiv'>");
	    response.getWriter().append("<h1>" + settitlep + "</h1>");
	    response.getWriter().append("<span class='pbtime'>(" + setpublish_timep + ")</span>");
	    response.getWriter().append("<p>" + setcontentp + "</p>");
	    response.getWriter().append("<span class='liang'>阅读量(" + setread_countp + ")</span><span class='liang ping'>评论量(" + setcomment_countp + ")</span>");
	    response.getWriter().append("</div>");
	    response.getWriter().append("<br><br><br><br><br><br>");
	%>
	<%
		int ii = 1;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection connh;
		try {
			connh = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			Statement stmth = connh.createStatement();
			String sqlha = "select * from review where bei_id = " + bkidt;
			ResultSet rsha = stmth.executeQuery(sqlha);
			while (rsha.next()) {
				hashMapa.put("review_id" + ii, rsha.getString("id"));
				hashMapa.put("reviewer" + ii, rsha.getString("reviewer"));
				hashMapa.put("revew_time" + ii, rsha.getString("revew_time"));
				hashMapa.put("review_content" + ii, rsha.getString("review_content"));
				hashMapa.put("bei_id" + ii, rsha.getString("bei_id"));
				hashMapa.put("ping_id" + ii, rsha.getString("ping_id"));
				System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
				System.out.println(hashMapa.get("review_id" + ii));
				System.out.println(hashMapa.get("reviewer" + ii));
				System.out.println(hashMapa.get("revew_time" + ii));
				System.out.println(hashMapa.get("review_content" + ii));
				System.out.println(hashMapa.get("bei_id" + ii));
				System.out.println(hashMapa.get("ping_id" + ii));
				System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
				ii++;
			}
			rsha.close();
			stmth.close();
			connh.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (strSessionj == null) {
			response.getWriter()
					.append("<span class='pdbl'><a href='http://localhost:8089/user_login.html'>评论请登录</a></span>");
			int iii = 1;
			System.out.println("ii:" + ii);
			while (iii < ii) {
				response.getWriter().append("<div class='plfk'>");
				response.getWriter().append("<div class='plfkleft'>");
				response.getWriter().append("<h2 align='center'>" + hashMapa.get("reviewer" + iii) + "</h2>");
				response.getWriter().append("</div>");
				response.getWriter().append("<span class='plfkright'>");
				response.getWriter().append("<p>" + hashMapa.get("review_content" + iii) + "</p>");
				response.getWriter().append("</span>");
				response.getWriter().append("<span class='pltimesj'>");
				response.getWriter().append(hashMapa.get("revew_time" + iii));
				response.getWriter().append("</span>");
				response.getWriter().append("</div>");
				iii++;
			}
		} else {
			int iii = 1;
			System.out.println("ii:" + ii);
			while (iii < ii) {
				response.getWriter().append("<div class='plfk'>");
				response.getWriter().append("<div class='plfkleft'>");
				response.getWriter().append("<h2 align='center'>" + hashMapa.get("reviewer" + iii) + "</h2>");
				if(hashMapa.get("reviewer" + iii).equals(strSessionj)){
				    response.getWriter().append("<input type='button' value='删除评论' class='deletepl shanp'/>");
				    System.out.println("************************************");
				    System.out.println("review_id:" + hashMapa.get("review_id" + iii));
				    System.out.println("************************************");
				    response.getWriter().append("<span class='sures' onclick='chufaz()' id='" + hashMapa.get("review_id" + iii) + "'>确认删除</span>");
				}
				response.getWriter().append("</div>");
				response.getWriter().append("<span class='plfkright'>");
				response.getWriter().append("<p>" + hashMapa.get("review_content" + iii) + "</p>");
				response.getWriter().append("</span>");
				response.getWriter().append("<span class='pltimesj'>");
				response.getWriter().append(hashMapa.get("revew_time" + iii));
				response.getWriter().append("</span>");
				response.getWriter().append("</div>");
				iii++;
			}
			response.getWriter().append("<br><form id='ping-form' method='post' class='pdbl'>");
			response.getWriter().append("<textarea name='ping_content' rows='10' cols='85'></textarea><br><br>");
			response.getWriter().append("<span class='aniu'><a id='savep' href='javascript:;'>发表评论</a></span>");
			response.getWriter().append("</form>");
			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
			} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
				e.printStackTrace();
			}
			Connection connhc;
			try {
				connhc = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
				Statement stmthc = connhc.createStatement();
				String sqlhd = "select count(bei_id) from review where bei_id = " + bkidt;
				ResultSet rshd = stmthc.executeQuery(sqlhd);
				int setnumhd = 0;
				while (rshd.next()) {
					int getnumhd = rshd.getInt(1);
					setnumhd = getnumhd;
				}
				PreparedStatement psthc;
				String sqlhc = "update blog set comment_count = " + setnumhd + " where id = ?";
				psthc = connhc.prepareStatement(sqlhc);
				psthc.setString(1, bkidt);
				psthc.executeUpdate();
				psthc.close();
				rshd.close();
				stmthc.close();
				connhc.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	%>
	<!--  
	<form id='ping-form' method='post' class='pdbl'>
		<textarea name='content' rows='10' cols='50'></textarea><br><br>
		<span class='aniu'><a id='save' href='javascript:;'>发表评论</a></span>
	</form>
	-->
	<form method="post" name="getdeleidpl">
		<input type='hidden' name="setdeleidpl">
	</form>
    <%
	    String txtoptionspl = request.getParameter("setdeleidpl"); 
	    System.out.println(txtoptionspl);
	    if(txtoptionspl != null){
	    	try {
			    Class.forName("com.mysql.jdbc.Driver").newInstance();
		    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			    e.printStackTrace();
		    }
		    Connection connstpl = null;
		    PreparedStatement pstpl = null;
		    try {
			    connstpl = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
			    System.out.println("***********************************");
			    String sqlpl = "delete from review where id = ?";
				pstpl = connstpl.prepareStatement(sqlpl);
				pstpl.setString(1, txtoptionspl);
				pstpl.executeUpdate();
			    System.out.println("***********************************");
			    pstpl.close();
			    connstpl.close();
			    System.out.println("blog id:" + setidididp);
			    System.out.println("blog type_id:" + settypeidp);
			    HttpSession sessionpla = request.getSession();
		        sessionpla.setAttribute("pldebkid", setidididp);  
		        sessionpla.setAttribute("pldebklxid", settypeidp);  
		        response.sendRedirect("/linshi.jsp");
		    } catch (SQLException e) {
			    e.printStackTrace();
		    }
	    }
    %>
    <!--  
    <span>鼠标当前在:</span>
	<span id="show"></span>
	-->
	<script type='text/javascript' src='/static/js/jquery.min.js'></script>
	<script type='text/javascript'>
	var zzjj = "0";
	$(function(){
		$('#savep').on('click',function(){
			$.post('/addping',$('#ping-form').serialize(),function(datag){
				alert("评论成功");
				console.log(datag);
				window.location.reload();
			});
		});
	});
	function sxpage(){
		window.location.href=window.location.href;
	}
	$(document).ready(function(){
		$(".sures").hide();
	    $(".shanp").click(function(){
	        $(".sures").toggle();
	        })
	});
	function fnGetTable(oEl) {
		try {
			while (null != oEl && oEl.tagName != "SPAN") {
				oEl = oEl.parentElement;
			}
			return oEl;
		} catch (e) {
			return null;
		}
	}
	function pos() {
		try {
			oElement = document.elementFromPoint(event.x, event.y);
			oElement = fnGetTable(oElement);
			if (oElement.tagName == "SPAN")
				zzjj = oElement.id;
		} catch (e) {
		}
	}
	function setdeleidpl(setdeleidpl){
		document.getdeleidpl.setdeleidpl.value = setdeleidpl;
	    document.getdeleidpl.submit();
	}
	function chufaz(){
		//show.innerText = oElement.id;
		setdeleidpl(zzjj);
	}
	</script>
</body>
</html>